package com.project.nhantvt.MBean;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.primefaces.event.RowEditEvent;

import com.project.entity.Staff;
import com.project.nhantvt.SessionBean.EmployeeBeanRemote;

@ManagedBean(name="mStaff")
@SessionScoped
public class MStaff {
	
	private Staff staff;

	private List<Staff> allStaff;

	public MStaff() {
		staff = new Staff();
	}
	public Staff getStaff() {
		return staff;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

	public void setAllStaff(List<Staff> allStaff) {
		this.allStaff = allStaff;
	}

	public List<Staff> getAllStaff() throws Exception {
		if (allStaff == null) {
			Context ctx = new InitialContext();
			EmployeeBeanRemote accRe = (EmployeeBeanRemote) ctx
					.lookup("MySweetHome/EmployeeBean/remote");

			allStaff = accRe.GetAllStaff();
			if (allStaff.size() != 0) {
				return allStaff;
			} else
				return null;
		} else {
			return this.allStaff;
		}
	} 
	
	public void AddNewStaff() throws Exception {
		Context ctx = new InitialContext();
		EmployeeBeanRemote accRe = (EmployeeBeanRemote) ctx
				.lookup("MySweetHome/EmployeeBean/remote");
		
		FacesContext context = FacesContext.getCurrentInstance();
		
		List emailCheck = accRe.CheckEmail(this.staff.getEmail());
		
		if(emailCheck.size() > 0)
		{
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Error",
					"This email already registered!"));
		}
		else
		{
			boolean result = false;
			InitialContext context1 = new InitialContext();
			
			EmployeeBeanRemote beanRemote = (EmployeeBeanRemote) context1
					.lookup("MySweetHome/EmployeeBean/remote");
			
			result = beanRemote.InsertStaff(getStaff());
		
			if (result) {
				context.addMessage(null, new FacesMessage(
						FacesMessage.SEVERITY_INFO, "Successful",
						"Insert Sucessful!"));
				
				staff = new Staff();
				
				String viewID = context.getViewRoot().getViewId();
				ViewHandler handler = context.getApplication().getViewHandler();
				UIViewRoot root = handler.createView(context, viewID);
				root.setViewId(viewID);
				context.setViewRoot(root);
			}
			else
				context.addMessage(null, new FacesMessage(
						FacesMessage.SEVERITY_ERROR, "Error", "Insert failed"));
		}
	}
	
	public void update() throws Exception {
		InitialContext ctx = new InitialContext();
		EmployeeBeanRemote accRe = (EmployeeBeanRemote) ctx
				.lookup("MySweetHome/EmployeeBean/remote");

		FacesContext context = FacesContext.getCurrentInstance();

		boolean check = accRe.UpdateStaff(getStaff());

		if (check == false)
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error", "Update failed"));
	}
	
	public void updateRow(RowEditEvent evt) throws Exception {
		InitialContext ctx = new InitialContext();
		EmployeeBeanRemote staffRe = (EmployeeBeanRemote) ctx
				.lookup("MySweetHome/EmployeeBean/remote");
		Staff rowStaff = (Staff) evt.getObject();
		
		FacesContext context = FacesContext.getCurrentInstance();

		boolean check = staffRe.UpdateStaff(rowStaff);

		if (check) {
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_INFO, "Successful",
					"Update Sucessful!"));
		}
		
		else
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Error", "Update failed"));
	}
	
	public String validStatus(boolean status) {
		if (status==true) 
			return "Activated";
		else
			return "Deactivated";
	}
	
}
